home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Utilities / WhosOnFirst / Source / InfoMgr.m < prev    next >
Encoding:
Text File  |  1994-05-23  |  10.9 KB  |  420 lines

  1. #import "InfoMgr.h"
  2. #import "DefaultMgr.h"
  3. #import "myMainObject.h"
  4. #import <stdlib.h>
  5. #import <appkit/Application.h>
  6. #import <appkit/Window.h>
  7. #import <appkit/View.h>
  8. #import <appkit/Control.h>
  9. #import <appkit/ActionCell.h>
  10. #import <appkit/TextField.h>
  11. #import <appkit/Button.h>
  12. #import <appkit/OpenPanel.h>
  13. #import <appkit/Matrix.h>
  14. #import <sys/param.h>
  15.  
  16. id infoManager;
  17. /*===========================================================================
  18.  
  19.     File: InfoMgr.m
  20.  
  21.     Purpose: This file is not just an infoManager.  It is an 
  22.         Info/Preferences manager.  However, due to the constant 
  23.         revisions (and no real code re-organizations) this file is 
  24.         now the multi-purpose beast that it is.  Re-organization
  25.         may take place at some later date.
  26.  
  27. ===========================================================================*/
  28.  
  29. @implementation InfoMgr
  30.  
  31. - init
  32. {
  33.     [super init];
  34.  
  35.     generalView = infoView;        /* See nib files for this one */
  36.     return self;
  37. }
  38.  
  39. /*===========================================================================
  40.  
  41.     Method: initDefaults
  42.  
  43.     Purpose: To get the defaults from the default manager into instance
  44.          variables and interface objects.
  45.  
  46.     Called: From appDidInit:sender
  47.  
  48. ===========================================================================*/
  49.  
  50. - initDefaults
  51. {
  52.     [speakMessages setStringValue:[defaultManager loginMessage] at:0];
  53.     [speakMessages setStringValue:[defaultManager logoutMessage] at:1];
  54.  
  55.     [whenToSpeak selectCellAt: [defaultManager whenToSpeak] :0];
  56.     [doubleClickAction selectCellAt: [defaultManager doubleClickAction] :0];
  57.  
  58.     [confirmDoubleClick setState:[defaultManager doubleClickConfirm]];
  59.  
  60.     [[speakLog cellAt:0 :0] setState:[defaultManager speakLogin]];
  61.     [[speakLog cellAt:0 :1] setState:[defaultManager speakLogout]];
  62.  
  63.     return self;
  64. }
  65.  
  66. /*===========================================================================
  67.  
  68.     Method: initSpeech: (const char *) dictPath
  69.  
  70.     Purpose: To allocate and initialize the speech object.  
  71.  
  72.     Algorithm: 
  73.         o Allocate and initialize speech object.
  74.         o Set the dictionary ordering to our preference
  75.         o set the Application dictionary path.
  76.  
  77.     NOTE:  The Application Dictionary (WhosOnFirst.preditor) is located
  78.         in WhosOnFirst.app/.  However, because the speech object is
  79.         communicating with a Speech Server, the server has no idea
  80.         where WhosOnFirst.app is located in the directory hierarchy.
  81.         Therefore, the full path to the dictionary file must be
  82.         provided to this method.  Please see myMainObject.m, 
  83.         -appDidInit:sender for more code.  See TextToSpeech Kit
  84.         documentation for a description of the setAppDictPath: 
  85.         method.
  86.  
  87.         Compilation of this method is conditional.  See 
  88.         Makefile.preamble to set up conditional compilation flags.
  89.  
  90. ===========================================================================*/
  91. #ifdef SPEECH
  92.  
  93. - initSpeech:(const char *) dictPath
  94. {
  95. short dictOrder[5] = { TTS_NUMBER_PARSER, TTS_USER_DICTIONARY, TTS_APPLICATION_DICTIONARY,
  96.             TTS_MAIN_DICTIONARY, TTS_LETTER_TO_SOUND};
  97.  
  98.     if (!mySpeech) mySpeech = [[TextToSpeech alloc] init];
  99.     if (mySpeech == nil)
  100.     {
  101.         sleep(5);
  102.         mySpeech = [[TextToSpeech alloc] init];        /* Avoid high load problems */
  103.     }
  104.     if (mySpeech == nil)
  105.     {
  106.         if ([[whenToSpeak selectedCell] tag]!=SPEECHOFF)
  107.         {
  108.             if (NXRunAlertPanel("TextToSpeech","Could not launch TextToSpeech Server", "Disable Speech",
  109.                             "Ignore", NULL)==1) [whenToSpeak selectCellAt: 0 :0];
  110.         }
  111.     }
  112.     else
  113.     {
  114.         [mySpeech setDictionaryOrder: dictOrder];
  115.         [mySpeech setAppDictPath: dictPath];
  116.     }
  117.     return self;
  118. }
  119.  
  120. #endif
  121.  
  122. /*===========================================================================
  123.  
  124.     Method: cleanUp
  125.  
  126.     Purpose: To update the defaults manager in preparation for 
  127.         application termination.
  128.  
  129. ===========================================================================*/
  130. - cleanUp
  131. {
  132. int temp;
  133.  
  134.     temp = [[whenToSpeak selectedCell] tag];
  135.     [defaultManager setWhenToSpeak:temp];
  136.  
  137.     temp = [[doubleClickAction selectedCell] tag];
  138.     [defaultManager setDoubleClickAction:temp];
  139.  
  140.     [defaultManager setLoginMessage: [speakMessages stringValueAt:0]];
  141.     [defaultManager setLogoutMessage: [speakMessages stringValueAt:1]];
  142.  
  143.     [defaultManager setSpeakLogin: (BOOL)[[speakLog cellAt:0 :0] state]];
  144.     [defaultManager setSpeakLogout: (BOOL)[[speakLog cellAt:0 :1] state]];
  145.     
  146.     [defaultManager setDoubleClickConfirm: (BOOL)[confirmDoubleClick state]];
  147.  
  148.  
  149.     return self;
  150. }
  151.  
  152. /*===========================================================================
  153.  
  154.     Method: switchViews:sender
  155.  
  156.     Purpose: To switch the view hierarchy based on the selection of the
  157.         pop-up menu on the info panel
  158.  
  159. ===========================================================================*/
  160.  
  161. - switchViews:sender
  162. {
  163.     [window disableFlushWindow];
  164.     [generalView removeFromSuperview];
  165.     switch ([[sender selectedCell] tag])
  166.     {
  167.         case 0:
  168.             [[window contentView] addSubview:infoView];
  169.             generalView = infoView;
  170.             break;
  171.         case 1:
  172.             [[window contentView] addSubview:speechView];
  173.             generalView = speechView;
  174.             break;
  175.         case 2:
  176.             [[window contentView] addSubview:speechControlView];
  177.             generalView = speechControlView;
  178.             break;
  179.         case 3:
  180.             [[scrollView docView] display];
  181.             [[window contentView] addSubview:iconInfoView];
  182.             generalView = iconInfoView;
  183.             break;
  184.         case 4:
  185.             [[window contentView] addSubview:TextToSpeechView];
  186.             generalView = TextToSpeechView;
  187.             break;
  188.         case 5:
  189.             [[window contentView] addSubview:LicenceView];
  190.             generalView = TextToSpeechView;
  191.             break;
  192.     }
  193.     [[window contentView] display];
  194.     [window reenableFlushWindow];
  195.     [window flushWindow];
  196.  
  197.     return self;
  198. }
  199.  
  200. /*===========================================================================
  201.  
  202.     Method: doubleClickEvent
  203.  
  204.     Purpose: An IconView will receive any double click events.  This
  205.         method is here to provide IconView with information about 
  206.         what is to be done in the event of a double click.
  207.  
  208. ===========================================================================*/
  209.  
  210. -(int) doubleClickEvent
  211. {
  212.     return([[doubleClickAction selectedCell] tag]);
  213. }
  214.  
  215. /*===========================================================================
  216.  
  217.     Method: confirmDoubleClick
  218.  
  219.     Purpose: If the user has requested confirmation of Double Click
  220.         events, display a panel with message supplied in the variable
  221.         "message" return the value returned by the panel.
  222.  
  223.         If the user has not requested confirmation of double clicks,
  224.         simply return true.
  225.  
  226. ===========================================================================*/
  227.  
  228. -(int) confirmDoubleClick:(const char *) message
  229. {
  230.  
  231.     if (![confirmDoubleClick state])
  232.         return(1);
  233.     else
  234.         return(NXRunAlertPanel("Double Click Confirmation", message, "Ok", "Cancel", NULL));
  235. }
  236.  
  237. #ifndef SPEECH
  238. - enableSpeech:sender
  239. {
  240.     return self;
  241. }
  242. #endif
  243.  
  244. /*===========================================================================
  245.  
  246.     Compilation of the following methods is conditional.  See 
  247.     Makefile.preamble to set up conditional compilation flags.
  248.  
  249. ===========================================================================*/
  250.  
  251. #ifdef SPEECH
  252.  
  253. - enableSpeech:sender
  254. {
  255. char dictPath[256];
  256. const char *appPath;
  257.  
  258.     if (mySpeech) return self;
  259.     if ([[whenToSpeak selectedCell] tag] != SPEECHOFF)
  260.     {
  261.         appPath = [mainObject appDirectory];
  262.         strcpy(dictPath, appPath);
  263.         strcat(dictPath, "/WhosOnFirst.preditor");
  264.  
  265.         /* Initialize the speech. Include the application dictionary.*/
  266.         [self initSpeech:dictPath];
  267.     }
  268.     return self;
  269. }
  270.  
  271.  
  272. /*===========================================================================
  273.  
  274.     Method:speakLoginMessage
  275.  
  276.     Purpose: Given the user's preference settings, speak the appropriate
  277.         login message.
  278.  
  279. ===========================================================================*/
  280.  
  281. - speakLoginMessage:(const char *) user tty:(const char *) tty host:(const char *) host
  282. {
  283. char tempHostName[MAXHOSTNAMELEN];
  284.  
  285.     if ([[speakLog cellAt:0 :0] state])            /* Speak login messages? */
  286.         switch([[whenToSpeak selectedCell] tag])    /* Get message from info panel */
  287.         {
  288.             case SPEECHOFF:             /* No Speech */
  289.                     break;
  290.  
  291.             case ANYUSER:                /* Speak for any user */
  292.                     [self speakFormatString: [speakMessages stringValueAt:0]
  293.                         name: user tty:tty host:host];
  294.  
  295.                     break;
  296.  
  297.             case OTHERUSERS:            /* Speak for other users */
  298.                     if (strcmp(getlogin(), user))
  299.                         [self speakFormatString: [speakMessages stringValueAt:0]
  300.                             name: user tty:tty host:host];
  301.                     break;
  302.  
  303.             case REMOTEUSERS:             /* Speak for remote users */
  304.                     gethostname(tempHostName, MAXHOSTNAMELEN);
  305.                     if (strcmp(host, tempHostName))
  306.                         [self speakFormatString: [speakMessages stringValueAt:0]
  307.                             name: user tty:tty host:host];
  308.                     break;
  309.  
  310.         }
  311.     return self;
  312. }
  313.  
  314. /*===========================================================================
  315.  
  316.     Method:speakLogoutMessage
  317.  
  318.     Purpose: Given the user's preference settings, speak the appropriate
  319.         logout message.
  320.  
  321. ===========================================================================*/
  322.  
  323. - speakLogoutMessage:(const char *) user tty:(const char *) tty host:(const char *) host
  324. {
  325. char tempHostName[MAXHOSTNAMELEN];
  326.  
  327.     if ([[speakLog cellAt:0 :1] state])            /* Speak logout messages? */
  328.         switch([[whenToSpeak selectedCell] tag])
  329.         {
  330.             case SPEECHOFF:             /* No Speech */
  331.                     break;
  332.  
  333.             case ANYUSER:                /* Speak for any user */
  334.                     [self speakFormatString: [speakMessages stringValueAt:1]
  335.                         name: user tty:tty host:host];
  336.  
  337.                     break;
  338.  
  339.             case OTHERUSERS:            /* Speak for other users */
  340.                     if (strcmp(getlogin(), user))
  341.                         [self speakFormatString: [speakMessages stringValueAt:1]
  342.                             name: user tty:tty host:host];
  343.                     break;
  344.  
  345.             case REMOTEUSERS:            /* Speak for remote users */
  346.                     gethostname(tempHostName, MAXHOSTNAMELEN);
  347.                     if (strcmp(host, tempHostName))
  348.                         [self speakFormatString: [speakMessages stringValueAt:1]
  349.                             name: user tty:tty host:host];
  350.                     break;
  351.  
  352.         }
  353.     return self;
  354. }
  355. /*===========================================================================
  356.  
  357.     Method: SpeakFormatString: format
  358.             name: user
  359.             tty: tty
  360.             host: host
  361.  
  362.     Purpose: Given that a message is to be spoken, get the message format
  363.         string from the info panel and construct a speak message
  364.         which conforms to the format string.
  365.  
  366. ===========================================================================*/
  367.  
  368. - speakFormatString:(const char *) format name:(const char *) user tty:(const char *) tty host:(const char *) host
  369. {
  370. int i, j = 0, k = 0;
  371. char finalString[1024];
  372.  
  373.     bzero(finalString, 1024);
  374.  
  375.     i = strlen(format);
  376.     while(j<i)
  377.     {
  378.         switch(format[j])
  379.         {
  380.             case '%':switch(format[j+1])
  381.                  {
  382.                     /* Insert login name */
  383.                     case 'u': strncat(finalString, user, 8);
  384.                           j+=2;
  385.                           break;
  386.  
  387.                     /* Insert host name */
  388.                     case 'h': strcat(finalString, host);
  389.                           j+=2;
  390.                           break;
  391.  
  392.                     /* Insert tty name */
  393.                     case 't': strcat(finalString, tty);
  394.                           j+=2;
  395.                           break;
  396.  
  397.                     /* A single % sign. */
  398.                     default: finalString[k] = '%';
  399.                          break;
  400.                  }
  401.                  k = strlen(finalString);
  402.                  break;
  403.  
  404.             default: finalString[k++] = format[j++];
  405.                  break;
  406.         }
  407.     }
  408.  
  409.  
  410.     if (!mySpeech) [self enableSpeech:self];
  411.     /* Here we are!  Actually tell the speech object to Speak the message :-) */
  412.     [mySpeech speakText:finalString];
  413.  
  414.     return self;
  415. }
  416.  
  417. #endif
  418.  
  419. @end
  420.